package cn.hutool.core.util;


import cn.hutool.core.lang.Validator;

import java.util.HashMap;
import java.util.Map;

/**
 * 车牌相关工具类
 *
 */

public class CarPlateUtil {

	/**
	 * 车牌中的省代码表
	 */

	private static final Map<String, String> CITY_CODES = new HashMap<>();

	static {
		CITY_CODES.put("京","北京");
		CITY_CODES.put("津","天津");
		CITY_CODES.put("沪","上海");
		CITY_CODES.put("渝","重庆");
		CITY_CODES.put("冀","河北");
		CITY_CODES.put("豫","河南");
		CITY_CODES.put("云","云南");
		CITY_CODES.put("辽","辽宁");
		CITY_CODES.put("黑","黑龙江");
		CITY_CODES.put("湘","湖南");
		CITY_CODES.put("皖","安徽");
		CITY_CODES.put("鲁","山东");
		CITY_CODES.put("新","新疆");
		CITY_CODES.put("苏","江苏");
		CITY_CODES.put("浙","浙江");
		CITY_CODES.put("赣","江西");
		CITY_CODES.put("鄂","湖北");
		CITY_CODES.put("桂","广西");
		CITY_CODES.put("甘","甘肃");
		CITY_CODES.put("晋","山西");
		CITY_CODES.put("蒙","内蒙古");
		CITY_CODES.put("陕","陕西");
		CITY_CODES.put("吉","吉林");
		CITY_CODES.put("闽","福建");
		CITY_CODES.put("贵","贵州");
		CITY_CODES.put("粤","广东");
		CITY_CODES.put("青","青海");
		CITY_CODES.put("藏","西藏");
		CITY_CODES.put("川","四川");
		CITY_CODES.put("宁","宁夏");
		CITY_CODES.put("琼","海南");
	}

	/**
	 * 根据车牌号号获取户籍省份编码
	 *
	 * @param carPlate 车牌号
	 * @return 省份编码
	 */
	public static String getProvinceCodeByCarPlate(String carPlate) {
		if(Validator.isPlateNumber(carPlate)){
			return carPlate.substring(0, 1);
		}
		return null;
	}

	/**
	 * 根据车牌号获取户籍省份
	 *
	 * @param carPlate 车牌号
	 * @return 省份名称。
	 */
	public static String getProvinceByCarPlate(String carPlate) {
		final String code = getProvinceCodeByCarPlate(carPlate);
		if (StrUtil.isNotBlank(code)) {
			return CITY_CODES.get(code);
		}
		return null;
	}
}
